<#include "common/common.html"/>
<#include "common/kindeditor.html"/>

<table
	     class="easyui-datagrid"   
	     id="datagrid"  
         title="资金管理"  
         url="${base}/fundBack/queryList?my=${my}"
         toolbar="#searchtool,#toolbar" 
         pagination="true"
         rownumbers="true" 
         fitColumns="false"
         singleSelect="true
         data-options="fit:false,border:false,pageSize:20,pageList:[5,10,15,20]" >  
    <thead>  
        <tr>
            <th field="id" width="5%" checkbox="true"></th>  
            <th field="titlesVal" width="10%">资金名称</th>
            <th field="fundNo" width="3%">资金编号</th>  
            <th field="industryName" width="8%">投资行业</th>
            <th field="destinationName" width="8%">投资地区</th>  
            <th field="userPhone" width="5%">手机号</th> 
            <th field="userName" width="3%">发布人</th>
            <th field="total" width="3%">投资金额（万）</th>
            <th field="investMode" formatter="investModeType" width="3%">项目类型</th>
            <th field="auth" formatter="formatauth" width="3%">认证状态</th>  
            <th field="pushTime" width="8%">发布时间</th>  
            <th field="descrition" width="4%">资金简介</th>  
            <th field="requirement" width="4%">备注</th>  
            <th field="operatorNames" width="3%">操作员</th>   
            <th field="state" formatter="formatFundState" width="5%">资金状态</th> 
            <th data-options="field:'_operate',width:275,align:'center',formatter:formatOper">操作</th>  
            <th data-options="field:'userId',width:85,align:'center',hidden:true" ></th>
            <th data-options="field:'industryId',width:85,align:'center',hidden:true" ></th>
            <th data-options="field:'operatorId',width:85,align:'center',hidden:true" ></th> 
            <th data-options="field:'destination',width:85,align:'center',hidden:true" ></th>   
            <th data-options="field:'intentionName',width:85,align:'center',hidden:true" ></th> 
            <th data-options="field:'intention',width:85,align:'center',hidden:true" ></th> 
        </tr>  
    </thead>  
</table>  



<div id="searchtool" style="padding:5px">  
<form id="searchForm" method="post">
  <span>关键字搜索:</span><input type="text" id="keyword" name="keyword" placeholder="资金名称/发布人/资金编号" value="" size=20 /> 
  <span>投资行业:</span> <input  class="easyui-combobox" name="industryId"   
    data-options="valueField:'id',textField:'industryName',panelHeight:'auto',editable:false,url:'${base}/industry/querySquAll',loadFilter:function(data){
    			    data.unshift({id:'',industryName:'全部'});
    			    return data;
    		      }," />  
  <span>投资地区:</span><input  class="easyui-combobox" name="destination"   
              data-options="valueField:'postcode',textField:'cityName',editable:false,url:'${base}/cityinfo/findAllProvince', loadFilter:function(data){
    			    data.unshift({postcode:'0',cityName:'全国'});
    			    return data;
    		      }" />  
  <span>资金状态:</span><select  class="easyui-combobox" name="state" editable="false">   
				<option value="">全部</option> 
				<option value="202">已提交</option> 
				<option value="203">已发布</option> 
				<option value="204">已撤回</option>
               </select>  
  <span>开始时间:</span><input name="startTime" class="easyui-datebox" data-options="formatter:myformatter,parser:myparser" style="width:200px" >  
  <span>结束时间:</span><input name="endTime" class="easyui-datebox"  data-options="formatter:myformatter,parser:myparser" style="width:200px" >  
  <span>项目类型:</span>
               <input  class="easyui-combobox" name="investMode"   editable="false"
    data-options="valueField:'itemCode',textField:'itemName',loadFilter:investModeFilter,url:'${base}/dictionary/initByParentCode?parentCode=300',loadFilter:function(data){
    			    data.unshift({itemCode:'',itemName:'全部'});
    			    return data;
    		      },"  />  
    
  <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-search" onclick="javascript:doSearch('searchForm');">搜索</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-search" onclick="javascript:$('#searchForm').form('clear');">清空</a>  
  </form>
</div>
<div id="toolbar">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="javascript:add('新增','fundBack/edit','fund-dlg','fund-fm',0);">新增</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="javascript:edit('编辑','fundBack/edit','fund-dlg','fund-fm',1);">编辑</a>

        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="javascript:deleteFund();">删除</a>
</div> 
<div id="fund-dlg" class="easyui-dialog"  style="padding:10px 20px;" height="700px" width="700px" resizable="true"  closed="true"  buttons="#dlgfund-buttons">
        <form id="fund-fm" method="post">
             <input type="hidden" name="intention" id="intention">
             <input type="hidden" name="state" id="state">
             
            <div class="fitem">
               <label>资金名称:</label>
               <input   id="titlesVal" name="titlesVal" required="true"  />

	      <!--   </div>
            <div class="fitem"> -->
                <label>资金编码:</label>  
                <input name="fundNo" id="fundNo"  disabled="disabled" >
            </div>
            <div class="fitem">
                <label>认证状态:</label>
                <select class="easyui-combobox"  name="auth" id="auth"  style="width:165px;" editable="false" required="true" >
                   <option value="503" >未认证</option>
	               <option value="502">实地认证</option> 
                </select>
        <!--    </div>
            <div class="fitem"> -->
                <label>投资地区:</label>
                <input name="destination" id="destination"  >
            </div>
            <div class="fitem">
                <label>投资行业:</label>
                <input name="industryId" id="industryId"  >
         <!--    </div>
            <div class="fitem"> -->
                <label>项目类型:</label>
                <input id="investMode" class="easyui-combobox" name="investMode" required="true" validType="notemptyorblank"  data-options="valueField:'itemCode',textField:'itemName',loadFilter:investModeFilter,url:'${base}/dictionary/initByParentCode?parentCode=300'" />   
            </div>
            <div class="fitem">  
                <label>发布人帐号:</label>
                <input name="userPhone" id="userPhone"  required="true" validType="notemptyorblank"  data-options="validType:['integer']">
          <!--   </div>
            <div class="fitem"> -->
                <label>资金规模(万):</label>
                <input name="total" id="total"  required="true" validType="notemptyorblank" data-options="validType:['integer']">
            </div>
            <div class="fitem">
                <label>关联意向:</label>
                <input id="intentionName" name="intentionName"  onclick="javascript:selIntention();"  />
                <input id="intentionNameHid" name="intentionNameHid"  readonly="readonly" />
                
            </div>
            <div class="fitem">
                <label>资金简介:</label>
                <textarea name="descrition" id="descrition" rows="5" style="width: 100px" size=10 required="true"></textarea>
            </div>
            <div class="fitem">
                <label>备注:</label>
                 <textarea  name="requirement" id="requirement" style="width: 300px" ></textarea>
            </div>
            
        </form>
</div>
<div id="dlgfund-buttons">
    <a href="javascript:void(0)" id="saveBtn" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="javascript:submitFund('fund-dlg','fund-fm')" style="width:90px">提交</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#fund-dlg').dialog('close')" style="width:90px">取消</a>
</div>


<div id="preView" class="easyui-window"  style="padding:10px 20px" closed="true" modal="true">
</div>
<div id="intentionDiv" class="easyui-window"  style="padding:10px 20px" closed="true" modal="true">
</div>

<script type="text/javascript">
function investModeType(val){
	
	if (val == 301){
		return '股权';
	} else if(val == 302){
		return '债权';
	}else if(val == 303){
		return '资产交易';
	}else if(val == 304){
		return '政府招商';
	}else {
		return '未知';
	}
}

function formatauth(val){
	if (val == 501){
		return '已认证';
	} else if(val == 502){
		return '实地认证';
	}else {
		return '未认证';
	}
}

function clearKindEditorValue(){
	KindEditor.html("#requirement","");
	KindEditor.html("#descrition","");
}
var requirementEditor=null;
var descritionEditor =null;
function initKindEditor(){
	if(!requirementEditor){
	 requirementEditor = KindEditor.create('textarea[id="requirement"]', {
		allowFileManager : true,
		cssPath : prettifyCssPath,
		uploadJson : uploadJson,
		urlType : 'absolute',
		items:kdItems,		
	});
	}
	
	if(!descritionEditor){
	 descritionEditor = KindEditor.create('textarea[id="descrition"]', {
		allowFileManager : true,
		cssPath : prettifyCssPath,
		uploadJson : uploadJson,
		urlType : 'absolute',
		items:kdItems,		
	});
	}
	
	prettyPrint();
}

function investModeFilter(data){
	 data.unshift({itemCode:'0',itemName:''});
	   return data;
}


//不用
function checkFundNo(){
	var fundNo = $('#fundNo').val();
	if(null!=fundNo && fundNo!=""){
		var urlstr = $("#base").val()+"/fundBack/checkFundNo?fundNo="+fundNo;
	    $.post(urlstr,function(result){
	       	var result = eval('('+result+')');
	       	if (result.errorMsg){
	       		   $("#fundNo").textbox('setValue','');
	        	   $.messager.alert('错误',"资金编码必须唯一",'error');
	           } 
	       });
	}

}

$(function(){
/* 	$('#suzuki').combobox({   
        url:'dictionary/getDictionaryByParentCode?parentCode=800',   
        editable:false, //不可编辑状态  
        cache: false,  
        //panelHeight: '150',//自动高度适合  
        valueField:'itemCode',     
        textField:'itemName'  ,
     });  */
	
    $("#userPhone").textbox({onChange:function(){
    	checkPhones();
    }});

});


function deleteFund(){
	var rows = $('#datagrid').datagrid('getSelections');
   if (rows.length>0){
   	var ids = [];
   	for(var i=0; i<rows.length; i++){
   		ids.push(rows[i].id);
   	}
   	url = base+"/fundBack/deleteFund?ids="+ids.join(",");
       $.messager.confirm('删除','确定要删除吗?',function(r){
           if (r){
               $.post(url,function(result){
               	var result = eval('('+result+')');
               	if (result.errorMsg){
                	   $.messager.alert('错误',"删除信息失败,请稍后再试",'error');
                	   $('#datagrid').datagrid('reload');   
                   } else if(result.infoMsg){
                	   $.messager.alert('提示',"删除信息成功",'info');
                	   $('#datagrid').datagrid('reload');   
                   }else{
                      $('#datagrid').datagrid('reload');   
                   }
               });
           }
       });
   }else{
   	 $.messager.alert('提示','请选择一条信息进行操作','info');
   }

}

function checkPhones(){
	var userPhone = $('#userPhone').val();
	if(null!=userPhone && userPhone!=""){
		var urlstr = $("#base").val()+"/fundBack/checkPhone?userPhone="+userPhone;
	    $.post(urlstr,function(result){
	       	var result = eval('('+result+')');
	       	if (result.errorMsg){
	       		   $("#userPhone").textbox('setValue','');
	        	   $.messager.alert('错误',"填写会员必须是注册会员",'error');
	           } 
	       });
	}

}



function updateStatus(title,status){
	var rows = $('#datagrid').datagrid('getSelections');
   if (rows.length>0){
   	var ids = [];
   	for(var i=0; i<rows.length; i++){
   		ids.push(rows[i].id);
   	}
   	url = base+"/fundBack/updateStatus?ids="+ids.join(",")+"&status="+status;
       $.messager.confirm(title,'确定要'+title+'吗?',function(r){
           if (r){
               $.post(url,function(result){
               	var result = eval('('+result+')');
               	if (result.errorMsg){
                	   $.messager.alert('错误',"修改信息状态失败,请稍后再试",'error');
                	   $('#datagrid').datagrid('reload');   
                   } else if(result.infoMsg){
                	   $.messager.alert('提示',"修改信息状态成功",'info');
                	   $('#datagrid').datagrid('reload');   
                   }else{
                      $('#datagrid').datagrid('reload');   
                   }
               });
           }
       });
   }else{
   	 $.messager.alert('提示','请选择一条信息进行操作','info');
   }

}


function add(titles,actUrl,dlgId,fmId,addOrUpdate){
	   
	centerDialog(dlgId).dialog('open').dialog('setTitle',titles);
	clearKindEditorValue();
	initKindEditor();
	
	if(fmId){
		$("#"+fmId).form('clear');
	}
	
	$('#destination').combobox({   
	    url: base+'/cityinfo/findAllProvince',    
	    required: true,
	    valueField:'postcode',
	    textField:'cityName',
	    multiple:true,
	    editable:false,
	    required:true,
	    width:165,
	    loadFilter:function(data){
		    data.unshift({postcode:'0',cityName:'全国'});
		    return data;
	      },
	    onSelect:function(record){
	    	var nodes = $('#destination').combobox('getValues');
	    	if(record.postcode == 0 ){
	    		$.each(nodes, function(i,n){
	    			if(n != 0){
	    				 $('#destination').combobox('unselect', n);
	    			}
   	    		});
	    	}else{
	    		$('#destination').combobox('unselect', 0);
	    	}
	    }
	});
	  	
	$('#industryId').combobox({   
	    url: base+'/industry/queryAll',
	    valueField:'id',
	    textField:'industryName',
	    required: true,
	    editable:false,
	    multiple:true,
	    width:165,
	    onSelect:function(record){
	    	var nodes = $('#industryId').combobox('getValues');
	    	if(record.id == 97 ){
	    		$.each(nodes, function(i,n){
	    			if(n != 97){
	    				 $('#industryId').combobox('unselect', n);
	    			}
   	    		});
	    	}else{
	    		$('#industryId').combobox('unselect', 97);
	    	}
	    }
	});
	makeUnDisabled();
    url= base+"/"+actUrl;
}


function edit(titles,actUrl,dlgId,fmId,addOrUpdate){
	var rows = $('#datagrid').datagrid('getSelections');
    if (rows&&rows.length==1){
    	var states=rows[0].state;
   
    	$('#destination').combobox({   
    	    url: base+'/cityinfo/findAllProvince',    
    	    required: true,
    	    valueField:'postcode',
    	    textField:'cityName',
    	    multiple:true,
    	    editable:false,
    	    width:165,
    	    loadFilter:function(data){
    		    data.unshift({postcode:'0',cityName:'全国'});
    		    return data;
    	      },
    	    onSelect:function(record){
    	    	var nodes = $('#destination').combobox('getValues');
    	    	if(record.postcode == 0 ){
    	    		$.each(nodes, function(i,n){
    	    			if(n != 0){
   	    				 $('#destination').combobox('unselect', n);
    	    			}
	   	    		});
    	    	}else{
    	    		$('#destination').combobox('unselect', 0);
    	    	}
    	    }
    	});
    	  	
    	$('#industryId').combobox({   
    	    url: base+'/industry/queryAll',
    	    valueField:'id',
    	    textField:'industryName',
    	    required: true,
    	    editable:false,
    	    multiple:true,
    	    width:165,
    	    onSelect:function(record){
    	    	var nodes = $('#industryId').combobox('getValues');
    	    	if(record.id == 97 ){
    	    		$.each(nodes, function(i,n){
    	    			if(n != 97){
   	    				 $('#industryId').combobox('unselect', n);
    	    			}
	   	    		});
    	    	}else{
    	    		$('#industryId').combobox('unselect', 97);
    	    	}
    	    }
    	});

    	centerDialog(dlgId).dialog('open').dialog('setTitle',titles);
    	makeUnDisabled();
    	$("#"+fmId).form('clear');
    	
    	$("#"+fmId).form('load',rows[0]);
    	clearKindEditorValue();
     	//单词是错的，坑啊
     	var desc =rows[0].descrition;
     	console.log(rows[0]);
     //	$("#descrition").val(desc);
     	//$("#requirement").val(rows[0].requirement);
     	KindEditor.html("#requirement",rows[0].requirement);
     	KindEditor.html("#descrition",rows[0].descrition);

    	//判断条件是否readonly
        if(203==states){
        	makeDisabled();
        }
    	
        url = base+"/"+actUrl+"?id="+rows[0].id+"&addOrUpdate="+addOrUpdate;
    }else{
    	$.messager.alert('提示','请选择一条数据进行操作','info');
    }
    
    initKindEditor();
}

var $win;
function selIntention(){
   var windowWidth =$(window.parent).width();
   var windowHeight=$(window.parent).height();
   var width= windowWidth-100;
   var height=windowHeight-200;
   var top=(windowHeight-height)/2;
   var left=(windowWidth-width)/2;
    $win = $('#intentionDiv').window({
    	href: base+'/project/selfund',
        title: '选择意向',
        width: width,
        height: height,
        top: top,
        left: left,
        shadow: true,
        modal: true,
        iconCls: 'icon-add',
        closed: true,
        minimizable: false,
        maximizable: false,
        collapsible: false
    });

    $win.window('open');
}

function doSelIntention(){
	var rows = $('#datagrid2').datagrid('getSelections');
    if (rows&&rows.length==1){
     	$('#intention').val(rows[0].id);
    	$("#userPhone").textbox('setValue',rows[0].phone);
    	$("#intentionName").val(rows[0].intro); 
    	
    	
    	$win.window('close');
    }else{
    	$.messager.alert('提示','请选择一条数据进行操作','info');
    }
}

function makeDisabled(){
	 $('#titlesVal').textbox({    
		 disabled:true
	 });
	 
	 $('#userPhone').textbox({    
		 disabled:true
	 });
	 $('#total').textbox({    
		 disabled:true
	 });
	 $('#intentionName').hide();
	 $('#intentionNameHid').val($('#intentionName').val());
	 $('#intentionNameHid').show();
	 $('#investMode').combobox("disable");
	 $('#auth').combobox("disable");
	 $('#destination').combobox("disable");
	 $('#industryId').combobox("disable");
}

function makeUnDisabled(){
	 $('#titlesVal').textbox({    
		 disabled:false
	 });
	 
	 $('#userPhone').textbox({    
		 disabled:false
	 });
	 $('#total').textbox({    
		 disabled:false
	 });

	 $('#intentionName').show();
	 $('#intentionNameHid').hide();
	 $('#investMode').combobox("enable");
	 $('#intentionName').show();
	// $('#intentionName').attr("disabled",false);
	// $('#intentionName').removeAttr("disabled");
	 $('#auth').combobox("enable");
	 $('#destination').combobox("enable");
	 $('#industryId').combobox("enable");

}

var $wins;
function showUser(id){
   var rect=centerRect(window);
    $wins = $('#intentionDiv').window({
    	href: base+'/userStatistic/showUser?id='+id+'&noCache=random',
        title: '显示关注人',
        width: rect.width,
        height: rect.height,
        top: rect.top,
        left: rect.left,
        shadow: true,
        modal: true,
        iconCls: 'icon-add',
        closed: true,
        minimizable: false,
        maximizable: false,
        collapsible: false
    });
    $('#datagrid').datagrid('clearSelections');
    $wins.window('open');
}

function formatOper(val,row,index){
	var viewPhotoUrl ="fundBack/viewPhoto?id="+row.id;
	var managePhotoUrl ="fundBack/managePhoto?id="+row.id;
	
	var viewPhoto = "<a href='javascript:open(\""+viewPhotoUrl+"\");'>图集</a>";
	var managePhoto = "<a href='javascript:open(\""+managePhotoUrl+"\");'>|图集管理</a>";
	var marryProject = "<a href='javascript:showMarryProject(\""+row.id+"\");'>匹配</a>";
	var showUser = "<a href='javascript:showUser(\""+row.id+"\");'>收藏人</a>";
	//发布状态下，图片管理不显示
	if(row.state == 203){
		managePhoto="";
	}
    return '<a  href="javascript:showAppFund('+index+')">APP预览</a> |'+viewPhoto +managePhoto + '|'+marryProject+'|'+showUser;  
    }
    
    
var $winst;
function showMarryProject(id){
   var windowWidth =$(window.parent).width();
   var windowHeight=$(window.parent).height();
   var width= windowWidth-200;
   var height=windowHeight-200;
   var top=(windowHeight-height)/2;
   var left=(windowWidth-width)/2;
    $winst = $('#intentionDiv').window({
    	href: base+'/fundBack/showMarryProject?id='+id+'&noCache=random',
        title: '匹配管理',
        width: width,
        height: height,
        top: top,
        left: left,
        shadow: true,
        modal: true,
        iconCls: 'icon-add',
        closed: true,
        minimizable: false,
        maximizable: false,
        collapsible: false
    });
    $('#datagrid').datagrid('clearSelections');
    $winst.window('open');
}

function showAppFund(index){
	$('#datagrid').datagrid('selectRow',index);// 关键在这里  
	
    var rows = $('#datagrid').datagrid('getSelections');
    if (rows.length>0){
    var href= '${mobileDomain}/fund/detail?id='+rows[0].id+'&noCache=random';
    var windowWidth =$(window.parent).width();
    var windowHeight=$(window.parent).height();
    var width=380;
    var height=windowHeight-200;
    var top=(windowHeight-height)/2;
    var left=(windowWidth-width)/2;
    var $win2 = $('#preView').window({
   	   content : '<iframe scrolling="yes" frameborder="0"  src="'  
              + href  
              + '" style="width:100%;height:98%;"></iframe>',  
        title: 'APP预览',
        width: width,
        height: height,
        top: top,
        left: left,
        shadow: true,
        modal: true,
        closed: true,
        minimizable: false,
        maximizable: false,
        collapsible: false
    });

    $win2.window('open');
    }
}



function submitFund(dlgId, fmId) {
   console.log("submit");
   makeUnDisabled();
	$("#" + fmId).form('submit', {
		url : url,
		onSubmit : function() {			
			$("#requirement").val(requirementEditor.html());
			$("#descrition").val(descritionEditor.html());
			$("#fundNo").attr("disabled",false);
			return $(this).form('validate');
		},
		success : function(result) {
			var result = eval('(' + result + ')');
			$("#" + dlgId).dialog('close');
			if (result.success) {
				$.messager.alert('提示', '资金管理编辑成功', 'info');
				$('#datagrid').datagrid('reload');
			} else {
				if (result.msg) {
					$.messager.alert('错误', '资金管理编辑失败', 'error');
				} else if (result.infoMsg) {
					$.messager.alert('提示', '资金管理编辑成功', 'info');
				} else {
					$('#datagrid').datagrid('reload');
				}
			}
		}
	});
}
</script>
